{include file="public/header" type='pear-container'/}
 	
 	<!--显示订单数据表格-->
 	<div class="layui-card">
 		<div class="layui-card-body">
 			<table id="dataTable" lay-filter="dataTable"></table>
 		</div>
 	</div>
    
    <!--根据条件执行对应删除操作-->
 	<script type="text/html" id="toolbar">
 		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        添加
		</button>
		<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		  </button>
 	</script>
    <script type="text/html" id="status">
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="status" {{# if(d.status==1){ }} checked {{# } }}>
    </script>
 	<script type="text/html" id="options">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
 	{include file="public/footer"/}
 	<script>
 		layui.use(['table', 'form', 'jquery'], function() {
 			let table = layui.table;
 			let form = layui.form;
 			let $ = layui.jquery;
 			let MODULE_PATH = "{$Request.root}/ypay.paylist/";

 			let cols = [
 				[{
 						type: 'checkbox'
 					}, {
 						field: "type",
 						title: "通道类型",
 						minWidth: 200,
 						unresize: "true",
 						align: "center",
                        templet: function (d) {
                           var type = null;
                           switch(d.type){
                                case "epay":
                                   type = "易支付";
                                   break;
                                case "dmf":
                                   type = "当面付";
                                   break;
                                case "wxpay":
                                   type = "微信官方";
                                   break;
                                case "alipay":
                                   type = "支付宝官方";
                                   break;
                           }
                           return type;
                       }
 					}, {
 						field: "name",
 						title: "通道名称",
 						minWidth: 200,
 						unresize: "true",
 						align: "center"
 					},{
 						field: "status",
 						title: "状态",
 						unresize: "true",
 						align: "center",
 						templet:"#status"
 					}, {
 						field: "create_time",
 						title: "创建时间",
                        minWidth: 150,
 						align: "center"
 					},{
                        title: '操作',
                        toolbar: '#options',
                        unresize: true,
                        align: 'center',
                        width:180,
                    }
 				]
 			]

 			table.render({
 				elem: '#dataTable',
 				url: MODULE_PATH + 'index',
 				page: true,
 				cols: cols,
 				cellMinWidth: 100,
 				skin: 'line',
 				toolbar: '#toolbar',
 				defaultToolbar: [{
 					title: '刷新',
 					layEvent: 'refresh',
 					icon: 'layui-icon-refresh',
 				}, 'filter', 'print', 'exports']
 			});


 			table.on('toolbar(dataTable)', function(obj) {
 				if (obj.event === 'add') {
 					window.add();
 				}else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }
 			});
 			
 			table.on('tool(dataTable)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });

 			form.on('submit(query)', function(data) {
 				table.reload('dataTable', {
 					where: data.field,
 					page: {
 						curr: 1
 					}
 				})

 				return false;
 			});
            
            form.on("switch(status)", function(data) {
                   var status = data.elem.checked?1:2;
                   var id = this.value;
                   var load = layer.load();
                   $.post(MODULE_PATH + "status",{status:status,id:id},function (res) {
                       layer.close(load);
                       //判断有没有权限
                       if(res && res.code==999){
                           layer.msg(res.msg, {
                               icon: 5,
                               time: 2000, 
                           })
                           return false;
                       }else if (res.code==200){
                           layer.msg(res.msg,{icon:1,time:1500})
                       } else {
                           layer.msg(res.msg,{icon:2,time:1500},function () {
                               $(data.elem).prop("checked",!$(data.elem).prop("checked"));
                               form.render()
                           })
                       }
                   })
               });
            
 			//弹出窗设置 自己设置弹出百分比
 			function screen() {
 				if (typeof width !== 'number' || width === 0) {
 					width = $(window).width() * 0.6;
 				}
 				if (typeof height !== 'number' || height === 0) {
 					height = $(window).height() * 0.6;
 				}
 				return [width + 'px', height + 'px'];
 			}

 			window.add = function(obj) {
 				layer.open({
 					type: 2,
 					maxmin: true,
 					title: '添加通道',
 					shade: 0.1,
 					area: screen(),
 					content: MODULE_PATH + 'add'
 				});
 			}
 			
 			window.edit = function(obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '修改通道信息',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'edit/id/'+obj.data['id']
                });
            }

            window.remove = function(obj) {
                layer.confirm('确定要删除该通道嘛?', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'remove',
                        data:{id:obj.data['id']},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }
            
            window.batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要删除这些登录配置嘛?', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'batchRemove',
                        data:{ids:ids},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

 			window.refresh = function(param) {
 				table.reload('dataTable');
 			}
 		})
 	</script>
 </body>
 </html>
